package com.javamsg.beans;

import static com.javamsg.CommonConstant.CUSTOM_CONFIG_FILE;
import static com.javamsg.CommonConstant.DEFAULT_BUFFER_SIZE;
import static com.javamsg.CommonConstant.DEFAULT_ICON;
import static com.javamsg.CommonConstant.P_AVATAR_ICON;
import static com.javamsg.CommonConstant.P_ENCODING;
import static com.javamsg.CommonConstant.P_NICKNAME;
import static com.javamsg.CommonConstant.VERSION;
import static com.javamsg.CommonConstant.DEFAULT_ENCODING;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.log4j.Logger;

public class SystemData {
	
	private static final Logger logger = Logger.getRootLogger(); 
	private static SystemData data = new SystemData();
	
	private String version ;
	private String sender ;
	private String host ;
	private String nickName ;

	private String avatarIcon ;
	
	private String operatingSystemVersion ;
	private String encoding ;
	
	private final String systemPath;
	
	private int bufferSize ;
	private Properties pros;
	private File customFile;

	private SystemData(){
		init();
		this.systemPath = System.getProperty("user.dir");
		logger.debug("user.dir : " + systemPath);
	}
	public static SystemData instance(){
		return data;
	}
	/**
	 * init system data
	 */
	private void init(){
		logger.debug("Enter class SystemData method init : Initialize System Data.");
		try{
			// load system configuration file
			customFile = new File(CUSTOM_CONFIG_FILE);
			if(!customFile.exists()){
				// if config file doesn't exist, create it
				customFile.createNewFile();
			}
			InputStream in = new BufferedInputStream (new FileInputStream(customFile));
			pros = new Properties();
			pros.load(in);
			
			this.version	= VERSION;
			this.sender		= System.getProperty("user.name");
			this.host		= InetAddress.getLocalHost().getHostName();
			this.operatingSystemVersion	= System.getProperty("os.name") + "#" + System.getProperty("os.version");
			this.bufferSize = DEFAULT_BUFFER_SIZE;
			
			// load nick name from configuration file
			if(pros.getProperty(P_NICKNAME) == null){
				this.nickName = sender;
				pros.setProperty(P_NICKNAME, this.nickName);
			}else{
				this.nickName = pros.getProperty(P_NICKNAME);
			}
			// load user encoding from configuration file
			if(pros.getProperty(P_ENCODING) == null){
				this.encoding = DEFAULT_ENCODING;
				pros.setProperty(P_ENCODING, this.encoding);
			}else{
				this.encoding = pros.getProperty(P_ENCODING);
			}
			// load user avatar icon from configuration file
			if(pros.getProperty(P_AVATAR_ICON) == null){
				this.avatarIcon = DEFAULT_ICON;
				pros.setProperty(P_AVATAR_ICON, this.avatarIcon);
			}else{
				this.avatarIcon = pros.getProperty(P_AVATAR_ICON);
			}
				
			OutputStream out = new BufferedOutputStream (new FileOutputStream(customFile));
			pros.store(out, "");
			in.close();
			out.close();
		}catch(SecurityException se){
			logger.error("init system data failed", se);
		}catch(UnknownHostException ue){
			logger.error("init system data failed", ue);
		}catch(IOException ioe){
			logger.error("init system data failed", ioe);
		}
		logger.debug("sender : " + sender);
		logger.debug("host : " + host);
		logger.debug("osVersion : " + operatingSystemVersion);
		logger.debug("encoding : " + encoding);
		logger.debug("Exit class SystemData method init : Initialize System Data.");
	}
	
	private void storeProperties(String key, String value){
		try{
			OutputStream out = new BufferedOutputStream (new FileOutputStream(customFile));
			pros.setProperty(key, value);
			pros.store(out, "");
			out.close();
		}catch(IOException ioe){
			logger.error("Store properties failed.", ioe);
		}
	}
	
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getSender() {
		return sender;
	}
	public void setSender(String sender) {
		this.sender = sender;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
		storeProperties(P_NICKNAME, nickName);
	}
	public String getOperatingSystemVersion() {
		return operatingSystemVersion;
	}
	public void setOperatingSystemVersion(String operatingSystemVersion) {
		this.operatingSystemVersion = operatingSystemVersion;
	}
	public String getEncoding() {
		return encoding;
	}
	public void setEncoding(String encoding) {
		this.encoding = encoding;
		storeProperties(P_ENCODING, encoding);
	}
	public int getBufferSize() {
		return bufferSize;
	}
	public void setBufferSize(int bufferSize) {
		this.bufferSize = bufferSize;
	}
	public String getAvatarIcon() {
		return avatarIcon;
	}
	public void setAvatarIcon(String avatarIcon) {
		this.avatarIcon = avatarIcon;
		storeProperties(P_AVATAR_ICON, avatarIcon);
	}
	public String getSystemPath() {
		return systemPath;
	}
}
